package com.six.flowerShopper.service


import com.six.flowerShopper.entity.PurchaseHistory
import com.six.flowerShopper.mapper.PurchaseHistoryMapper
import com.six.library.model.Flowers
import com.six.library.model.ResultDataT
import com.six.library.utils.Constant
import com.six.library.utils.RestTemplateUtils
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import javax.annotation.Resource


/*-*- coding:utf-8 -*-
 * @Author  : debi
 * @Time    : 10/26/22
 * @Software: IntelliJ IDEA
 */
@Service
@Transactional

class PurchaseHistoryService {
    @Resource
    private lateinit var phm: PurchaseHistoryMapper

    @Resource
    private lateinit var rt: RestTemplateUtils

    fun findAll(): List<PurchaseHistory> {
        return phm.findAll()
    }

    /**
     * Get flower info by id
     * 通過鮮花Id 獲取鮮花信息
     * @param id
     */
    fun getFlowerInfoById(id: Int): Flowers? {
        val resultData = rt.getForObject<ResultDataT<Flowers>>(Constant.Flower, "/flower/selectById/${id}")
        if (resultData != null) {
            return resultData.data
        }
        return null
    }

    /**
     * Get total price
     * 設置購買總價
     * @param flowerId
     * @param ph
     * @return
     */
    fun getTotalPrice(flowerId: Int, ph: PurchaseHistory): Float {
        val resultData = getFlowerInfoById(flowerId)
        if (resultData != null) {
            val price = resultData.price
            return (price * ph.discount * ph.discount).toFloat()
        }
        return 0.0F

    }
}